<?php
            
use app\model\Theme; 



class Template_Page extends PluginWebPage {

    function main($method)
    {
        $method = 'template_main';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $list = WeikeModel::getTemplateList($themeid,$search_data,$page_start,$page_size);
        $count = WeikeModel::getTemplateList($themeid,$search_data,0,0);
        $total = $page_total = $count;
//        foreach ($list as $item){
//            $temp_config = json_decode($item['config'],true);
//            $item['config'] = $temp_config;
//        }
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/weike/template/main', $return_clicent);
    }

    function addpage(){
        $method = 'template_add';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $template_id = intval($_P['template_id'])?:0;
        $data = [];
        $temp_config = [];
        if($template_id){
            $data = WeikeModel::getTemplateInfoById($template_id,$themeid);
            $temp_config = $data['config'];
        }
        $return_clicent = get_defined_vars();
return $this->view('/weike/template/edit', $return_clicent);
    }

    function addinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $template_id = intval($_P['template_id'])?:0;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $name = trim($_P['name'])?:'';
        if (empty($name)){
            show_json(0,'模板名称不能为空！');
            return;
        }
        $status = intval($_P['status']);
        $data = [
            'name'=>$name,
            'status'=>$status,
            'themeid'=>$themeid,
            'is_delete'=>0,
        ];
        //模板配置信息:主题色、客片信息背景图、打call宝贝人气、打call宝贝人气图标、打call解锁礼品、打call解锁礼品图标、
        //打call中间背景、打call底部背景、打call按钮图片、送礼物广告图片、领取礼包广告图片、领取礼包悬浮按钮、
        //内容图中领取礼包按钮（图片、宽度百分比、距离顶部距离）、左侧按钮颜色、右侧按钮颜色、表单提交按钮颜色、特权礼包背景图、特权礼包文字颜色、倒计时背景图
        $template_config = [
            'wk_give_call_button_img'=>trim($_P['wk_give_call_button_img'])?tomedia($_P['wk_give_call_button_img']):'',//送礼物广告图片
            'wk_give_gift_ad_img'=>trim($_P['wk_give_gift_ad_img'])?tomedia($_P['wk_give_gift_ad_img']):'',//送礼物广告图片
            'wk_get_gift_ad_img'=>trim($_P['wk_get_gift_ad_img'])?tomedia($_P['wk_get_gift_ad_img']):'',//领取礼包广告图片
            'wk_get_gift_icon'=>trim($_P['wk_get_gift_icon'])?tomedia($_P['wk_get_gift_icon']):'',//领取礼包悬浮按钮
            'wk_gift_bag_bg'=>trim($_P['wk_gift_bag_bg'])?tomedia($_P['wk_gift_bag_bg']):'',//特权礼包背景图
            'wk_time_bg'=>trim($_P['wk_time_bg'])?tomedia($_P['wk_time_bg']):'',//倒计时背景图
            'wk_button_left_bg_color'=>trim($_P['wk_button_left_bg_color'])?:'#f36765',//左侧按钮背景颜色
            'wk_button_right_bg_color'=>trim($_P['wk_button_right_bg_color'])?:'#DC2A21',//右侧按钮背景颜色
            'wk_button_form_bg_color'=>trim($_P['wk_button_form_bg_color'])?:'#FF526D',//表单提交按钮背景颜色
            'wk_content_get_gift_button'=>trim($_P['wk_content_get_gift_button'])?tomedia($_P['wk_content_get_gift_button']):'',//内容图中button按钮图片
            'wk_content_get_gift_button_width'=>floatval($_P['wk_content_get_gift_button_width'])?:0,//内容图中button按钮宽
            'wk_content_get_gift_button_top'=>intval($_P['wk_content_get_gift_button_top'])?:0,//内容图中button按钮距离顶部
            'wk_user_info_bg' => trim($_P['wk_user_info_bg'])?tomedia($_P['wk_user_info_bg']):'https://css2020.yixuan.net/2020css/weike/images/icon11.png',//客片信息背景图
            'wk_call_left_bg' => trim($_P['wk_call_left_bg'])?tomedia($_P['wk_call_left_bg']):'https://css2020.yixuan.net/2020css/weike/images/icon3_01.png',//打call宝贝人气
            'wk_call_left_icon' => trim($_P['wk_call_left_icon'])?tomedia($_P['wk_call_left_icon']):'https://css2020.yixuan.net/2020css/weike/images/icon2.png',//打call宝贝人气图标
            'wk_call_right_bg' => trim($_P['wk_call_right_bg'])?tomedia($_P['wk_call_right_bg']):'https://css2020.yixuan.net/2020css/weike/images/icon16_01.png',//打call解锁礼品
            'wk_call_right_icon' => trim($_P['wk_call_right_icon'])?tomedia($_P['wk_call_right_icon']):'https://css2020.yixuan.net/2020css/weike/images/icon18.png',//打call解锁礼品图标
            'wk_call_center_bg' => trim($_P['wk_call_center_bg'])?tomedia($_P['wk_call_center_bg']):'https://css2020.yixuan.net/2020css/weike/images/icon3_hs_02.png',//打call中间背景
            'wk_call_bottom_bg' => trim($_P['wk_call_bottom_bg'])?tomedia($_P['wk_call_bottom_bg']):'https://css2020.yixuan.net/2020css/weike/images/icon3_hs_03.png',//打call底部背景
            'wk_gift_bags_text_color'=>trim($_P['wk_gift_bags_text_color'])?:'#D63471',//特权礼包文字颜色
            'wk_theme_color'=>trim($_P['wk_theme_color'])?:'#F7E6EA',//页面头部标题栏背景颜色与限量领取N张背景颜色
        ];
        if($template_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getTemplateInfoById($template_id,$themeid);
            if(empty($info)){
                show_json(0,'未找到模板信息');
                return;
            }
            $data['config'] = json_encode($template_config);
            $where = ['id'=>$template_id,'themeid'=>$themeid];
            //修改数据库字段
            $res = WeikeModel::editTemplateDataByWhere($where,$data);
            if (!$res){
                show_json(0,['message'=>'操作失败']);
                return;
            }else{
                show_json(1,['message'=>'操作成功','template_id'=>$template_id]);
                return;
            }
        }else{
            //新增
            $data['config'] = json_encode($template_config);
            //存入数据库
            $res = WeikeModel::insertTemplateData($data);
            if (!$res){
                show_json(0,['message'=>'操作失败']);
                return;
            }else{
                show_json(1,['message'=>'操作成功','template_id'=>$res]);
                return;
            }
        }
    }

    function deleteInfo(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $template_id = intval($_P['template_id']);
        if (!$template_id){
            show_json(0,'参数错误');
            return;
        }
        $info = WeikeModel::getTemplateInfoById($template_id,$themeid);
        if (empty($info)){
            show_json(0,'未找到模板信息');
            return;
        }
        if ($info['is_delete']){
            show_json(0,'微客已删除！');
            return;
        }
        $data['is_delete'] = 1;
        $where = ['id'=>$template_id,'themeid'=>$themeid];
        //修改数据库字段
        $res = WeikeModel::editTemplateDataByWhere($where,$data);
        if (!$res){
            show_json(0,['message'=>'操作失败']);
            return;
        }else{
            show_json(1,['message'=>'操作成功']);
            return;
        }
    }

}